<?php
include_once('configure.php');
include('class.db.php');
include('open-flash-chart.php');

$nome_sommita = 'valore';
$giorni=$_GET['giorni'];

$msql  = new Db;
$msql->connect();

$data_sec=strtotime( date("Y-m-d G:00:00"));
echo "\n$data_sec\n";
//$data_sec=strftime('%Y-%m-%d %H:%M:%S',$data_sec);

//$data_sec=$data_sec + 3600*$arr_str[0];

$data_sec=$data_sec - 3600*24*$giorni;
$data_less_hour=strftime('%Y-%m-%d %H:%M:%S',$data_sec);

$query="SELECT valore,DATE_FORMAT(data,'%e-%m-%X %H:%i')as data FROM rasponi where data >= '$data_less_hour' && $nome_sommita > -999";
$result_dati_impianto = $msql->execute($query);

$max=$min=0;
while( $row = mysql_fetch_array($result_dati_impianto) )
{
  $segnali[] = $row['valore' ];
  $segnali_pre[]='null';
  $date[] = $row['data'];
  
  $max=max($max,$row[$nome_sommita]);
  $min=min($min,$row[$nome_sommita]);
}

$query="SELECT valore,DATE_FORMAT(data,'%e-%m-%X %H:%i')as data FROM rasponi_tmp where data>= '".$data_less_hour."' && $nome_sommita > -999";
echo $query;
$result_simulazioni = $msql->execute($query) ;

while( $row = mysql_fetch_array($result_simulazioni) )
{
  $segnali_pre[] = $row['valore' ];
  $date[] = $row['data'];
  
  $max=max($max,$row[$nome_sommita]);
  $min=min($min,$row[$nome_sommita]);

}
//print_r($date);
//echo $query;

$msql->disconnettiDB();
$steps=6;

$offset=abs($max - $min);

$max_val=$max + (4*$offset);
$max_val=($max_val)?$max_val:1;
$min_val=$min - (4*$offset);
$min_val=($min_val)?$min_val:-1;
//$step_x=5*$giorni;
$numb=count($date);
$step_x=ceil($numb/5);

echo "@@$max_val@@$min_val@@$steps@@$offset";
// Graphic config

// use the chart class to build the chart:
$g = new graph();
$g->bg_colour = '#FFFFFF';

$g->title( 'Sezione ' .$nome_sommita. ' ' .date("d M Y"), '{color: #000000; font-size: 15px; text-align: center}' );

$g->set_x_label_style( 10, '#004080', 0, $step_x, '#80ff80' );  //$size integer, size in pixels of the X axis ticks.
//                                                         //$colour string, text colour.
//                                                         //$orientation integer, see X Axis Labels Orientation.
// ... and tick every 4 :                                  //$step integer, see X Axis Labels Step.
//                                                         //$grid_colour, see X Axis Labels Step.
$g->set_x_axis_steps( ceil($step_x/24));

$g->set_x_labels( $date );

$g->set_y_max(  ($max_val)); //Massimo Valore dell'asse Y da graficare
$g->set_y_min(  $min_val ); //Massimo Valore dell'asse Y da graficare

$g->y_label_steps( $steps ); //Step asse Y

$g->set_x_tick_size( 10 );
//$g->line_hollow( 2, 4, '#164166', 'Soglia', 10 );

$g->set_data( $segnali );
$g->line( 2, '#5B1D7A', 'Segnale ' .$nome_sommita,  12);


$g->set_data( $segnali_pre );
$g->line( 2, '#9933CC', 'Sezione'.$nome_sommita.' Previsionale', 12 );

//
// AXIS COLOURS
//
$g->x_axis_colour( '#818D9D', '#F0F0F0' );
$g->y_axis_colour( '#818D9D', '#ADB5C7' );
$g->y_right_axis_colour( '#164166' );

$g->set_tool_tip( '#key#<br>#val# (#x_label#)' );

$g->set_y_legend( 'Segnale cm', 9, '#00000C' );
$g->set_x_legend( 'Tempo', 9, '#00000C' );

$g->set_num_decimals( 3 );
$g->set_is_fixed_num_decimals_forced( true );
$g->set_is_decimal_separator_comma( true );
$g->set_is_thousand_separator_disabled( true ); 

// display the data
echo $g->render();

?>